﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Customers;
using MpSoft.Library.BUSS.Settings;
using MpSoft.Library.CommonLibrary;

namespace MpSoft.Web.Controls.Customer
{
    public partial class CartControl : System.Web.UI.UserControl
    {
        #region Var

        private double totalcart = 0;
        private bool existsCart = false;
        #endregion

        #region Method

        private void BindControl()
        {
            var listSession = Session["ListCart"];
            if (listSession != null)
            {
                var list = (List<Cart>)listSession;
                totalcart = OrderBUSS.GetTotalOrder(list);
                rplist.DataSource = list;
                rplist.DataBind();
                existsCart = true;
            }
        }

        protected string BindPaymentMethod()
        {
            var list = PaymentMethodBUSS.PaymentMethod_Select(-1, 1, true);
            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var obj in list)
                    sb.Append("<p><label for='m" + obj.PaymentMethodID + "'><input required name='payment' id='m" + obj.PaymentMethodID + "' type='radio' value='" + obj.PaymentMethodID + "'/>" + obj.Name + "</label></p>");
                return sb.ToString();
            }
            return "";
        }

        protected string BindShippingMethod()
        {
            var list = ShippingMethodBUSS.ShippingMethod_Select(-1, 1, true);
            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var obj in list)
                    sb.Append("<p><label for='p" + obj.ShippingMethodID + "'><input required name='shipping' id='p" + obj.ShippingMethodID + "' type='radio' value='" + obj.ShippingMethodID + "'/>" + obj.Name + "</label></p>");
                return sb.ToString();
            }
            return "";
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            BindControl();
        }

        protected void cmdComfirm_Click(object sender, EventArgs e)
        {
            var listSession = Session["ListCart"];
            if (listSession != null)
            {
                var list = (List<Cart>)listSession;
                int value = 0;
                totalcart = OrderBUSS.GetTotalOrder(list);
                Order order = new Order();
                int.TryParse(hdPayment.Value, out value);
                order.PaymentMethodID = value;
                int.TryParse(hdShipping.Value, out value);
                order.ShippingMethodID = value;
                order.FullName = txtFullName.Text;
                order.Email = txtEmail.Text;
                order.Address = txtAddress.Text;
                order.Phone = txtPhone.Text;
                order.Total = totalcart;
                var result = OrderBUSS.Order_Add(order, list);
                if (result > 0)
                {

                    var uc = Page.LoadControl(@"~/Controls/Customer/EmailOrderDetailControl.ascx") as EmailOrderDetailControl;
                    uc.OrderID = result;
                    Page page = new Page();
                    HtmlForm form = new HtmlForm();
                    form.Controls.Add(uc);
                    page.Controls.Add(form);
                    StringWriter writer = new StringWriter();
                    Server.Execute(page, writer, false);
                    HtmlString.SendMail(order.FullName, "Thông tin đặt mua hàng", writer.ToString(), order.Email);
                    Session["ListCart"] = null;
                    Response.Redirect("/xac-nhan-thanh-toan.html?order=" + result);
                }
            }
        }

        #endregion

        #region Properties

        public double TotalCart { get { return totalcart; } }

        public bool ExistsCart { get { return existsCart; } }
        #endregion
    }
}